/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.sys.util.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.richfaces.component.UITree;
import org.richfaces.event.TreeSelectionChangeEvent;
import org.richfaces.model.TreeNode;

/**
 *
 * @author Administrator
 */
public class TreeUtil {

    private static Integer level = 1;
    private static Map<String, List<TreeModel>> map;

    public static void addNode(List li, TreeNode rootNode, String[] rule) {
        level = 1;
        map = splitList(li, rule); //将li按层级(gradeLevel)分开
        List<TreeModel> ls = map.get(String.valueOf(1));//第一层list
        addNodes(ls, rootNode, rule);
    }

    private static void addNodes(List<TreeModel> ls, TreeNode rootNode, String[] rule) {
        String key;
        for (int i = 0; i < ls.size(); i++) {
            TreeModel node = ls.get(i);
            key = node.getCode().trim();//以代码作为树节点的键(键不能重复)
            level = node.getGrade() + 1;//子节点层级
            Boolean isleaf = 1 == node.getIsEnd(); //true : 是叶子节点
            MyTreeNodeImpl currentNode = new MyTreeNodeImpl(isleaf, node);
            rootNode.addChild(key, currentNode);
            if (!isleaf) {
                List<TreeModel> subli = buildSubli(map.get(String.valueOf(level)), node, rule);//找出当前节点子节点list
                addNodes(subli, currentNode, rule);
            }
        }
    }

    //子节点list
    private static List<TreeModel> buildSubli_(List<TreeModel> ls, TreeModel tm, String[] rule) {
        List<TreeModel> subli = new ArrayList();
        String str = "";
        for (TreeModel l : ls) {
            Integer len = 0;
            for (int i = 0; i < rule.length; i++) {
                len += Integer.parseInt(rule[i]);
                if (level == i + 2) {
                    str = l.getCode().substring(0, len);
                }
            }
            if (str.equals(tm.getCode().trim())) {
                subli.add(l);
            }
        }
        return subli;
    }

    //获取 传入节点的 直接子节点list
    private static List<TreeModel> buildSubli(List<TreeModel> subLevelNodes, TreeModel node, String[] rule) {
        String currentCode = node.getCode().trim();
        List<TreeModel> subli = new ArrayList();
        for (TreeModel subNodel : subLevelNodes) {
            String parentCode = parentCode(subNodel.getCode().trim(), rule);
            if (currentCode.equals(parentCode)) {
                subli.add(subNodel);
            }
        }
        return subli;
    }

    //将list按层级(gradeLevel)分开
    public static Map<String, List<TreeModel>> splitList(List<TreeModel> li, String[] rule) {
        Integer len = rule.length;
        Map<String, List<TreeModel>> maps = new HashMap<String, List<TreeModel>>();
        for (int i = 0; i < len; i++) {
            maps.put(String.valueOf(i + 1), new ArrayList<TreeModel>());
        }
        for (TreeModel tm : li) {
            inner:
            for (int i = 0; i < len; i++) {
                if (tm.getGrade() == i + 1) {
                    maps.get(String.valueOf(i + 1)).add(tm);
                    break inner;
                }
            }
        }
        return maps;
    }

    //树节点监听器
    public static Object listener(TreeSelectionChangeEvent event) {
        List<Object> selection = new ArrayList<Object>(event.getNewSelection());
        Object currentSelectionKey = selection.get(0);
        UITree tree = (UITree) event.getSource();
        tree.setRowKey(currentSelectionKey);
        return ((MyTreeNodeImpl) tree.getRowData()).getData();
    }

    //检查code是否合法,合法返回true
    public static boolean checkCodeLegal(String code, String[] rule) {
        code = code.trim();
        boolean legal = false;
        int len = 0;
        for (String ru : rule) {
            len += Integer.parseInt(ru);
            if (len == code.length()) {
                legal = true;
                break;
            }
        }
        return legal;
    }

    public static TreeModel parentNode(TreeModel currentNode, String[] rule) {
        TreeModel parentNode = new TreeModel();
        parentNode.setCode(parentCode(currentNode.getCode(), rule));
        parentNode.setGrade(currentNode.getGrade() - 1);
        parentNode.setIsEnd(0);
        return parentNode;
    }

    //获取父节点code
    public static String parentCode(String currCode, String[] rule) {
        currCode = currCode.trim();
        int parentCodeGrade = codeGrade(currCode, rule) - 1;
        int[] gradeLen = gradeLen(rule);
        if (parentCodeGrade > 0) {
            return currCode.substring(0, gradeLen[parentCodeGrade - 1]);
        }
        return "";
    }

    //确定节点层级
    public static int codeGrade(String code, String[] rule) {
        code = code.trim();
        int gradeLen = 0;
        int grade = 1;
        int codeLen[] = new int[rule.length];
        for (int i = 0; i < rule.length; i++) {
            gradeLen += Integer.parseInt(rule[i]);
            if (code.length() == gradeLen) {
                grade = i + 1;
                break;
            }
        }
        return grade;
    }

    //返回一级,二级... 代码长度
    public static int[] gradeLen(String[] rule) {
        int codeLen[] = new int[rule.length];
        int gradeLength = 0;
        for (int i = 0; i < rule.length; i++) {
            gradeLength += Integer.parseInt(rule[i]);
            codeLen[i] = gradeLength;
        }
        return codeLen;
    }
}
